package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.QuerySalaryData;
import com.boss.salary.service.IQuerySalaryDataService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestSchema(schemaId = "QuerySalaryDataController")
@RestController
@RequestMapping(value = "/QuerySalaryDataController", produces = MediaType.APPLICATION_JSON)
public class QuerySalaryDataController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(QuerySalaryDataController.class);


    @Autowired
    private IQuerySalaryDataService service;

    @Autowired
    private ICommonService iCommonService;

    /**
     * 获取工资支付数据
     * @param page
     * @param limit
    //     * @param salaryMonth
     * @param choiceOfTime
     * @return
     */
    @GetMapping("/queryPayrollDataPage")
    public ApiResultDTO queryPayrollDataPage(
            @RequestHeader("tokenid") String tokenid,
            @RequestParam(value = "page") String page,
            @RequestParam(value = "limit") String limit,
            @RequestParam(value = "agencyId") String agencyId,
            @RequestParam(value = "choiceOfTime") String choiceOfTime
    ){
        try {
            UserDTO userDto = getUser(tokenid);
            List<String> childrenIds = iCommonService.getAgencyChildrenId(userDto, agencyId);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            Map<String, Object> queryParam = new HashMap<>(1);
//            queryParam.put("salaryMonth", salaryMonth);
            queryParam.put("choiceOfTime", choiceOfTime);
            PageDTO<QuerySalaryData> QuerySalaryDataPageDTO = service.queryPayrollDataPage(paginationDTO, queryParam,childrenIds);
            return ApiResultDTO.success(QuerySalaryDataPageDTO,null);
        } catch (Exception e) {
            logger.error("获取工资支付数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }
    }
}
